import Vue from 'vue'
import VueRouter from 'vue-router'
// import {getToken} from '@/utils/auth'

Vue.use(VueRouter)

//禁止Router打印全局异常
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
};

//简化下方routes配置 + 懒加载
function route(path, file, name, children) {
	return {
		exact: true,
		path,
		name,
		children,
		component: (resolve) => require(['@/views' + file], resolve)
	}
}
//路由路径配置
const routes = [
	route("/", "/Home", "Home"),
	route("/about", "/About", "About")
]
//路由配置
const router = new VueRouter({
  // mode: 'history',    //把路径的#去掉,nginx还需要配置,否则404, 设置publicPath: '/'
  mode: 'hash',    //会有#号,设置publicPath: './'
  // base: process.env.BASE_URL,
  routes
})

//导航守卫
//使用 router.beforeEach 注册一个全局前置守卫,判断用户是否登录,以及对路由里没有配置的情况进行404处理
// router.beforeEach((to,from,next) => {
// 	if (to.matched.length === 0) {
// 		from.name ? next({
// 			name: from.name
// 		}) : next('/404');
// 	} else {
// 		if(to.path === '/login') {
// 			next();
// 		} else {
// 			let token = getToken();
// 			if(token === null || token === '' || token === undefined) {
// 				next('/login');
// 			} else {
// 				next();
// 			}
// 		}
// 	}
// })

export default router
